/**  
* @Title: ShopActivityController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月24日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.model.cityRun.CrShopActivityOut;
import com.lemon.model.cityRun.CrShopActivityOutDiscount;
import com.lemon.service.cityRun.ShopActivityService;
import com.lemon.utils.ReqResult;

/**
 * <p>Title: ShopActivityController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月24日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/act")
public class ShopActivityController {

	@Resource
	private ShopActivityService activityService;

	/**
	 * <b>Description:<blockquote> 查询活动列表
	 * </blockquote></b>
	 * 
	 * @Title: getRuleList
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月24日
	 */
	@RequestMapping("/getActivityList")
	public ReqResult getActivityList(@ModelAttribute CrShopActivityOut act , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			act.setWhere(" and a.status != 0 ");
			List<CrShopActivityOut> list = activityService.selectListBySelective(act);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//获取全部用户信息
	@RequestMapping("/getActivityById")
	public ReqResult getActivityById(@ModelAttribute CrShopActivityOut rule , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			CrShopActivityOut list = activityService.selectByPrimaryKey(rule.getId());
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改活动信息
	 * </blockquote></b>
	 * 
	 * @Title: updaActivityInfo
	 * @param rule
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月24日
	 */
	@LoginRequired
	@RequestMapping(value = "/updateActivityInfo.do" , method = RequestMethod.POST)
	public ReqResult updaActivityInfo(@ModelAttribute CrShopActivityOut rule , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopActivityOut activityOut = new CrShopActivityOut();
			activityOut.setShopId(rule.getShopId());
			activityOut.setActivityType(rule.getActivityType());
			List<CrShopActivityOut> list = activityService.selectListBySelective(activityOut);
			if (list != null && list.size() > 0) {
				//修改
				rule.setId(list.get(0).getId());
				int row2 = activityService.updateSelective(rule);
			} else {
				rule.setStatus(2);
				if (rule.getActivityType() == 3) {
					rule.setStatus(1);
				}
				//添加
				int row = activityService.insertSelective(rule);
			}
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("添加权限异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	////////////////////////单品折扣相关/////////////////////////////////
	@RequestMapping("/getActivityDisList")
	public ReqResult getActivityDisList(@ModelAttribute CrShopActivityOutDiscount dis , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			dis.setWhere(" and a.status != 0  and b.shop_id = " + IdTypeHandler.decode(request.getParameter("shopId")));
			List<CrShopActivityOutDiscount> list = activityService.selectDisListBySelective(dis);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping(value = "/addActivityDisInfo.do" , method = RequestMethod.POST)
	public ReqResult addActivityDisInfo(@ModelAttribute CrShopActivityOutDiscount dis , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//添加
			int row = activityService.insertDisSelective(dis);
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("添加异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping(value = "/editActivityDisInfo.do" , method = RequestMethod.POST)
	public ReqResult editActivityDisInfo(@ModelAttribute CrShopActivityOutDiscount dis , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//添加
			int row = activityService.editDisSelective(dis);
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("添加异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

}
